#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define Rep(i,n) for(int i=0;i<(n);++i) 
#define For(i,a,b) for(int i=(a);i<=(b);++i) 
#define ForI(TP,i,coll) for(TP (i)=coll.begin();(i)!=coll.end();(i)++)
#define ForD(i,a,b) for(int i=(a);i>=(b);--i)
#define All(a) a.begin(), a.end()

struct node
{
	static map<string, node*> insts;

	string name;
	node* link,*par;
	vector<node*> subs;
	node(const string& cname="", node* l=0)
	{
		name=cname;
		link=l;
		par=0;
	}
};
map<string, node*> node::insts;

static node* root;

int M,N;
node* createnode(const string& name)
{
	static string lk="(link)";
	bool isLK=true;

	if(name.size()<lk.size())
		isLK=false;
	else
	{
		for(int i=0;i<(int)lk.size();i++)
		{
			int i1=i;
			int i2=i+(name.size()-lk.size());
			if(name[i2]!=lk[i])
				isLK=false;
		}
	}

	node* link=0;
	if(isLK)
	{
		string nm=name.substr(0, name.size()-lk.size());
		link=node::insts[nm];
	}

	node* pn=new node(name, link);
	node::insts[name]=pn;

	return pn;
}
node* createnode2(node* link)
{
	node* pn=new node("", link);
	return pn;
}

// test
struct cell
{
	node* n;
	int l;
};
vector<cell> cls;

void gencells(node* n, int l)
{
	cell c={n, l};
	cls.push_back(c);
	Rep(i, n->subs.size())
		gencells(n->subs[i], l+1);
}
void re_gencells()
{
	cls.resize(0);
	gencells(root, 0);
}

node* get_node(int x, int y)
{
	if(y%10==0)// note
		return 0;
	int yi=y/10;
	if(yi<0 || yi>=cls.size())
		return 0;
	cell c=cls[yi];
	
	int l=(c.l+1)*10;
	int r=l+50;
	
	if(x>l && x<r)
		return c.n;
	return 0;
}

bool find_in(node* start, node* n)
{
	if(n==start) return true;
	Rep(i, start->subs.size())
	{
		if(find_in(start->subs[i], n))
			return true;
	}
	return false;
}

bool dfs_tc(node* n, set<node*>& ns)
{
	if(ns.find(n)!=ns.end())
		return true; // circle
	ns.insert(n);
	
	Rep(i, n->subs.size())
	{
		{
			node* pn=n->subs[i];
			if(dfs_tc(pn, ns))
				return true;
		}
	}
	if(n->link)
	{
		node* pn=n->link;
		if(dfs_tc(pn, ns))
			return true;
	}
	ns.erase(n);
	return false;
}

bool test_circle()
{
	set<node*> ns;
	return dfs_tc(root, ns);
	//set<node*> vis;

	//queue<node*> ns;
	//ns.push(root);

	//while(!ns.empty())
	//{
	//	node* fr=ns.front(); ns.pop();
	//	if(vis.find(fr)!=vis.end())
	//		return true;
	//	vis.insert(fr);

	//	Rep(i, fr->subs.size())
	//		ns.push(fr->subs[i]);
	//	if(fr->link)
	//		ns.push(fr->link);
	//}
	//return false;
}

int move(node* s, node* d)
{
	if(!s->par)
		return 2;
	if(d->link)
		return 2;
	if(find_in(s, d))
		return 2;

	node* op=s->par;
	vector<node*>::iterator it;
	it=find(All(op->subs), s);

	op->subs.erase(it);
	d->subs.push_back(s);

	if(test_circle())
	{
		op->subs.insert(it, s);
		d->subs.pop_back();
	}

	return 0;
}

int link(node* s, node* d)
{
	if(!s->par)
		return 2;
	if(d->link)
		return 2;
	if(find_in(s, d))
		return 2;

	node* pn=createnode2(s);
	d->subs.push_back(pn);

	if(test_circle())
	{
		d->subs.pop_back();
		delete pn;
		return 2;
	}
	return 0;
}


int main()
{
	{
		string line;
		getline(cin, line);
		istringstream in(line);
		in>>M>>N;
	}

	Rep(i, M)
	{
		string line;
		getline(cin, line);
		istringstream in(line);
		
		string pname;
		in>>pname;
		node* pn=node::insts[pname];
		if(!pn)
		{
			pn=createnode(pname);
			root=pn;
		}

		while(in)
		{
			string nname;
			in>>nname;
			if(!nname.empty())
			{
				pn->subs.push_back(createnode(nname));
				pn->subs.back()->par=pn;
			}
		}
	}

	Rep(i, N)
	{
		string line;
		getline(cin, line);
		istringstream in(line);

		char c;
		int x1, y1, x2, y2;
		in>> c >> x1 >> y1 >> x2 >> y2;

		re_gencells();
		node* s=get_node(x1, y1),*d=get_node(x2, y2);

		if(s==0 || d==0 || s==d)
			cout<< 1 <<endl;
		else
		{
			if(c=='L')
				cout<<move(s, d)<<endl;
			else
				cout<< link(s, d)<<endl;
		}
	}

	return 0;
}